/* $Id : user.js 4865 2007-01-31 14:04:10Z paulgao $ */

var lefttime = 0;

function checkRegister(type, mobile, mobile_captcha, password, com_name, com_tel) {
    if (Utils.trim(mobile) == '') {
        alert("请输入手机号");
        return false;
    } else {
        if (!(/^1[3|4|5|8][0-9]\d{8}$/.test(mobile))) {
            alert("请输入正确的手机号码。");
            return false;
        }
        //检查手机
        var result = Utils.trim(Ajax.call('index.php?action=user.checkmobile', 'mobile=' + mobile, false, 'GET', 'TEXT', false, false));

        if (result != 0) {
            if (result == 1)
                alert("该手机号码已被注册，请输入其他号码");
            else
                alert(result);
            return false;
        }
        
        if (type == '2') {
            // VIP会员校验
            if (Utils.trim(com_name) == '') {
                alert("请输入公司名称");
                return false;
            }
            if (Utils.trim(com_tel) == '') {
                alert("请输入公司电话");
                return false;
            } else {
                if (!(/^(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,8}$/.test(com_tel))) {
                    alert("请输入正确的公司电话");
                    return false;
                }
            }
        }
    }
    
    if (Utils.trim(mobile_captcha) == '') {
        alert("请输入短信验证码");
        return false;
    } else {
        var message = Utils.trim(Ajax.call('index.php?action=mobile.checkauthcode', 'mobile=' + mobile + '&authcode=' + mobile_captcha, false, 'GET', 'TEXT', false, false));

        if (message != 0) {
            switch (parseInt(message)) {
                case 1:
                    message = "您输入的短信验证码错误，请重试(最多10次)";
                    break;
                case 4:
                case 2:
                    message = "验证码已失效，请重新发送";
                    break;
                case 3:
                    message = "无效验证操作";
                    break;
            }
            alert(message);
            return false;
        }
    }
    if (Utils.trim(password) == '') {
        alert("请输入您的密码。");
        return false;
    } else {
        if (password.length < 6)
        {
            alert("密码长度不能少于6个字符。");
            return false;
        }
    }
    return true;
}

function getRegisterMobileCaptcha(mobile) {
    if (lefttime > 0) {
        alert(lefttime + "秒之后重试（本地）");
        return 0;
    }
    if (Utils.trim(mobile) == '') {
        alert("请输入手机号");
        return;
    }
    if (!(/^1[3|4|5|8][0-9]\d{8}$/.test(mobile))) {
        alert("请输入正确的手机号码。");
        return false;
    }

    //检查手机
    var result = Utils.trim(Ajax.call('index.php?action=user.checkmobile&mobile=' + mobile, '', false, 'GET', 'TEXT', false, false));

    if (result != 0) {
        if (result == 1)
            alert("该手机号码已被注册，请输入其他号码");
        else
            alert(result);
        return;
    }
    //发送短信
    var message = Utils.trim(Ajax.call('index.php?action=mobile.sendAuthcode&opmsg=注册&mobile=' + mobile, '', false, 'GET', 'TEXT', false, false));
    var ret = parseInt(message);
    if (ret != 1) {
        if (ret == 2)
            alert("一分钟之后重试");
        else
            alert(message);
        return;
    }
    msg = "已发送，1分钟后可重发短信。";
    $("#lefttime_tip").text(msg).show();
    $("#f_lefttime_tip").text(msg).show();

    alert('手机验证码已经发送到您的手机，您可以在1分钟后重发短信。');

    lefttime = 60;

    tt = 0;

    tt = setInterval(function() {
        lefttime--;

        if (lefttime >= 0) {
            $("#btn_get_register_mobile_captcha").attr('value', '重新获取(' + lefttime + ')');
            $("#f_btn_get_register_mobile_captcha").attr('value', '重新获取(' + lefttime + ')');
        } else {
            clearInterval(tt);
            $("#f_btn_get_register_mobile_captcha").attr('value', '重新获取');
        }

    }, 1000);
}

function checkRegisterMobile(mobile) {
    if (Utils.trim(mobile) == '') {
        $("#register_mobile_notice").text("请输入手机号");
        $("#f_register_mobile_notice").text("请输入手机号");
    } else {
        if (!(/^1[3|4|5|8][0-9]\d{8}$/.test(mobile))) {
            $("#register_mobile_notice").text("请输入正确的手机号码。");
            $("#f_register_mobile_notice").text("请输入正确的手机号码。");
            return false;
        }

        //检查手机
        var result = Utils.trim(Ajax.call('index.php?action=user.checkmobile', 'mobile=' + mobile, false, 'GET', 'TEXT', false, false));

        if (result != 0) {
            if (result == 1)
                result = "该号码已被注册";
            $("#register_mobile_notice").text(result);
            $("#f_register_mobile_notice").text(result);
        } else {
            $("#register_mobile_notice").text("可以注册");
            $("#f_register_mobile_notice").text("可以注册。");
        }
    }
}

function checkRegisterComTel(mobile) {
    if (Utils.trim(mobile) == '') {
        $("#register_com_tel_notice").text("请输入公司电话");
        $("#f_register_com_tel_notice").text("请输入公司电话");
    } else {
        if (!(/^(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,8}$/.test(mobile))) {
            $("#register_com_tel_notice").text("请输入正确的公司电话，3或4位区号-7位或8位电话号。");
            $("#f_register_com_tel_notice").text("请输入正确的公司电话。");
            return false;
        }
        $("#register_com_tel_notice").text("可以注册");
        $("#f_register_com_tel_notice").text("可以注册。");
    }
}

/* *
 * 修改会员信息
 */
function userEdit() {
    var frm = document.forms['formEdit'];
    var email = frm.elements['email'].value;
    var com_name = frm.elements['com_name'].value;
    var com_tel = frm.elements['com_tel'].value;
    var msg = '';
    var reg = null;
    var passwd_answer = frm.elements['passwd_answer'] ? Utils.trim(frm.elements['passwd_answer'].value) : '';
    var sel_question = frm.elements['sel_question'] ? Utils.trim(frm.elements['sel_question'].value) : '';
    if (com_name.length == 0)
        msg += "公司名不能空" + '\n';
    if (!(/^\d{4}-\d{7,8}$/.test(com_tel)))
        msg += "公司电话不正确" + '\n';
    if (email.length == 0) {
        msg += email_empty + '\n';
    } else {
        if (!(Utils.isEmail(email))) {
            msg += email_error + '\n';
        }
    }
    var mobile = frm.elements['mobile'].value;
    if (!(/^1[3|4|5|8][0-9]\d{8}$/.test(mobile)))
        msg += "请输入正确的手机号码\n";
    if (msg.length > 0) {
        alert(msg);
        return false;
    } else {
        return true;
    }
}

function RstPwdByMsg()
{
	if($("#img_authcode").val().length != 6)
	{
		alert('请输入6位验证码');
		return false;
	}
	return BaseCheckPwd();
}

function BaseCheckPwd()
{
    var frm = document.forms['formPassword'];
    var new_password = frm.elements['new_password'].value;
    var confirm_password = frm.elements['comfirm_password'].value;
    var msg = '';
    var reg = null;
    if (new_password.length == 0) {
        msg += new_password_empty + '\n';
    }
    if (confirm_password.length == 0) {
        msg += confirm_password_empty + '\n';
    }
    if (new_password.length > 0 && confirm_password.length > 0) {
        if (new_password != confirm_password) {
            msg += both_password_error + '\n';
        }
    }
    if (msg.length > 0) {
        alert(msg);
        return false;
    } else {
        return true;
    }
}

/* 会员修改密码 */
function editPassword()
{
    var frm = document.forms['formPassword'];
    var old_password = frm.elements['old_password'].value;
    var msg = '';
    if (old_password.length == 0) {
		alert(old_password_empty);
		return false;
    }
	return BaseCheckPwd();
}

/* *
 * 对会员的留言输入作处理
 */
function submitMsg() {
    var frm = document.forms['formMsg'];
    var msg_title = frm.elements['title'].value;
    var msg_content = frm.elements['content'].value;
    var msg = '';
    if (msg_title.length == 0) {
        msg += msg_title_empty + '\n';
    }
    if (msg_content.length == 0) {
        msg += msg_content_empty + '\n'
    }
    if (msg_title.length > 200) {
        msg += msg_title_limit + '\n';
    }
    if (msg.length > 0) {
        alert(msg);
        return false;
    } else {
        return true;
    }
}
/* *
 * 会员找回密码时，对输入作处理
 */
function submitPwdInfo() {
    var frm = document.forms['getPassword'];
    var user_name = frm.elements['user_name'].value;
    var email = frm.elements['email'].value;
    var authcode = frm.elements['authcode'].value;
    var errorMsg = '';
    auth2 = parseInt(authcode);
    if (isNaN(auth2) || authcode.length != 6 || auth2 < 100000 || auth2 > 999999)
        errorMsg += "验证码格式不正确\n";
    if (user_name.length == 0) {
        errorMsg += user_name_empty + '\n';
    }
    if (email.length == 0) {
        errorMsg += email_address_empty + '\n';
    } else {
        if (!(Utils.isEmail(email))) {
            errorMsg += email_address_error + '\n';
        }
    }
    if (errorMsg.length > 0) {
        alert(errorMsg);
        return false;
    }
    return true;
}
/* *
 * 会员找回密码时，对输入作处理
 */
function submitPwd() {
    var frm = document.forms['getPassword2'];
    var password = frm.elements['new_password'].value;
    var confirm_password = frm.elements['confirm_password'].value;
    var errorMsg = '';
    if (password.length == 0) {
        errorMsg += new_password_empty + '\n';
    }
    if (confirm_password.length == 0) {
        errorMsg += confirm_password_empty + '\n';
    }
    if (confirm_password != password) {
        errorMsg += both_password_error + '\n';
    }
    if (errorMsg.length > 0) {
        alert(errorMsg);
        return false;
    } else {
        return true;
    }
}
/* *
 * 处理会员提交的缺货登记
 */
function addBooking()
{
    var frm = document.forms['formBooking'];
    var goods_id = frm.elements['id'].value;
    var rec_id = frm.elements['rec_id'].value;
    var number = frm.elements['number'].value;
    var desc = frm.elements['desc'].value;
    var linkman = frm.elements['linkman'].value;
    var email = frm.elements['email'].value;
    var tel = frm.elements['tel'].value;
    var msg = "";
    if (number.length == 0) {
        msg += booking_amount_empty + '\n';
    } else {
        var reg = /^[0-9]+/;
        if (!reg.test(number)) {
            msg += booking_amount_error + '\n';
        }
    }
    if (desc.length == 0) {
        msg += describe_empty + '\n';
    }
    if (linkman.length == 0) {
        msg += contact_username_empty + '\n';
    }
    if (email.length == 0) {
        msg += email_empty + '\n';
    } else {
        if (!(Utils.isEmail(email))) {
            msg += email_error + '\n';
        }
    }
    if (tel.length == 0) {
        msg += contact_phone_empty + '\n';
    }
    if (msg.length > 0) {
        alert(msg);
        return false;
    }
    return true;
}
/* *
 * 会员登录
 */
function userLogin() {
    var frm = document.forms['formLogin'];
    var username = frm.elements['username'].value;
    var password = frm.elements['password'].value;
    var msg = '';
    if (username.length == 0 || username == '用户名 / 手机号 / 邮箱')
	{
        msg += username_empty + '\n';
    }
    if (password.length == 0) {
        msg += password_empty + '\n';
    }
    if (msg.length > 0) {
        alert(msg);
        return false;
    } else {
        return true;
    }
}
function chkstr(str) {
    alert(str);
    for (var i = 0; i < str.length; i++) {
        if (str.charCodeAt(i) < 127 && !str.substr(i, 1).match(/^\w+$/ig)) {
            return false;
        }
    }
    return true;
}
function f_check_password(password) {
    if (password.length < 6) {
        document.getElementById('f_password_notice').innerHTML = '密码不能少于6个字符';
    } else {
        document.getElementById('f_password_notice').innerHTML = '可以注册';
    }
}
function check_password(password) {
    if (password.length < 6) {
        document.getElementById('password_notice').innerHTML = "密码不能少于6个字符";
    } else {
        document.getElementById('password_notice').innerHTML = "可以注册";
    }
}

function f_check_conform_password(conform_password) {
    password = document.getElementById('f_password').value;
    if (conform_password.length < 6) {
        document.getElementById('f_confirm_password_notice').innerHTML = '密码不能少于6个字符';
        return false;
    }
    if (conform_password != password) {
        document.getElementById('f_confirm_password_notice').innerHTML = '两次输入密码不一致';
    } else {
        document.getElementById('f_confirm_password_notice').innerHTML = '可以注册';
    }
}

function check_conform_password(conform_password) {
    password = document.getElementById('password1').value;
    if (conform_password.length < 6) {
        document.getElementById('conform_password_notice').innerHTML = password_shorter;
        return false;
    }
    if (conform_password != password) {
        document.getElementById('conform_password_notice').innerHTML = confirm_password_invalid;
    } else {
        document.getElementById('conform_password_notice').innerHTML = msg_can_rg;
    }
}

/* *
 * 处理注册用户
 */
function register() {
    var frm = document.forms['formUser'];
    var username = Utils.trim(frm.elements['username'].value);
    var email = frm.elements['email'].value;
    var password = Utils.trim(frm.elements['password'].value);
    var checked_agreement = frm.elements['agreement'].checked;
    var msn = frm.elements['extend_field1'] ? Utils.trim(frm.elements['extend_field1'].value) : '';
    var qq = frm.elements['extend_field2'] ? Utils.trim(frm.elements['extend_field2'].value) : '';
    var home_phone = frm.elements['extend_field4'] ? Utils.trim(frm.elements['extend_field4'].value) : '';
    var office_phone = frm.elements['extend_field3'] ? Utils.trim(frm.elements['extend_field3'].value) : '';
    var mobile_phone = frm.elements['extend_field5'] ? Utils.trim(frm.elements['extend_field5'].value) : '';
    var passwd_answer = frm.elements['passwd_answer'] ? Utils.trim(frm.elements['passwd_answer'].value) : '';
    var sel_question = frm.elements['sel_question'] ? Utils.trim(frm.elements['sel_question'].value) : '';
    var msg = "";
    // 检查输入
    var msg = '';
    if (username.length == 0) {
        msg += username_empty + '\n';
    } else if (username.match(/^\s*$|^c:\\con\\con$|[%,\'\*\"\s\t\<\>\&\\]/)) {
        msg += username_invalid + '\n';
    } else if (username.length < 3) {
        //msg += username_shorter + '\n';
    }
    if (email.length == 0) {
        msg += email_empty + '\n';
    } else {
        if (!(Utils.isEmail(email))) {
            msg += email_invalid + '\n';
        }
    }
    if (password.length == 0) {
        msg += password_empty + '\n';
    } else if (password.length < 6) {
        msg += password_shorter + '\n';
    }
    if (/ /.test(password) == true) {
        msg += passwd_balnk + '\n';
    }

    if (checked_agreement != true) {
        msg += agreement + '\n';
    }
    if (msn.length > 0 && (!Utils.isEmail(msn))) {
        msg += msn_invalid + '\n';
    }
    if (qq.length > 0 && (!Utils.isNumber(qq))) {
        msg += qq_invalid + '\n';
    }
    if (office_phone.length > 0) {
        var reg = /^[\d|\-|\s]+$/;
        if (!reg.test(office_phone)) {
            msg += office_phone_invalid + '\n';
        }
    }
    if (home_phone.length > 0) {
        var reg = /^[\d|\-|\s]+$/;
        if (!reg.test(home_phone)) {
            msg += home_phone_invalid + '\n';
        }
    }
    if (mobile_phone.length > 0) {
        var reg = /^[\d|\-|\s]+$/;
        if (!reg.test(mobile_phone)) {
            msg += mobile_phone_invalid + '\n';
        }
    }
    if (passwd_answer.length > 0 && sel_question == 0 || document.getElementById('passwd_quesetion') && passwd_answer.length == 0) {
        msg += no_select_question + '\n';
    }
    for (i = 4; i < frm.elements.length - 4; i++) {
        // 从第五项开始循环检查是否为必填项
        needinput = document.getElementById(frm.elements[i].name + 'i') ? document.getElementById(frm.elements[i].name + 'i') : '';
        if (needinput != '' && frm.elements[i].value.length == 0) {
            msg += '- ' + needinput.innerHTML + msg_blank + '\n';
        }
    }
    if (msg.length > 0) {
        alert(msg);
        return false;
    } else {
        return true;
    }
}
/* *
 * 用户中心订单保存地址信息
 */
function saveOrderAddress(id) {
    var frm = document.forms['formAddress'];
    var consignee = frm.elements['consignee'].value;
    var email = frm.elements['email'].value;
    var address = frm.elements['address'].value;
    var zipcode = frm.elements['zipcode'].value;
    var tel = frm.elements['tel'].value;
    var mobile = frm.elements['mobile'].value;
    var sign_building = frm.elements['sign_building'].value;
    var best_time = frm.elements['best_time'].value;
    if (id == 0) {
        alert(current_ss_not_unshipped);
        return false;
    }
    var msg = '';
    if (address.length == 0) {
        msg += address_name_not_null + "\n";
    }
    if (consignee.length == 0) {
        msg += consignee_not_null + "\n";
    }
    if (msg.length > 0) {
        alert(msg);
        return false;
    } else {
        return true;
    }
}
/* *
 * 会员余额申请
 */
function submitSurplus() {
    var frm = document.forms['formSurplus'];
    var surplus_type = frm.elements['surplus_type'].value;
    var surplus_amount = frm.elements['amount'].value;
    var process_notic = frm.elements['user_note'].value;
    var payment_id = 0;
    var msg = '';
    if (surplus_amount.length == 0) {
        msg += surplus_amount_empty + "\n";
    } else {
        var reg = /^[\.0-9]+/;
        if (!reg.test(surplus_amount)) {
            msg += surplus_amount_error + '\n';
        }
    }
    if (process_notic.length == 0) {
        msg += process_desc + "\n";
    }
    if (msg.length > 0) {
        alert(msg);
        return false;
    }
    if (surplus_type == 0) {
        for (i = 0; i < frm.elements.length; i++) {
            if (frm.elements[i].name == "payment_id" && frm.elements[i].checked) {
                payment_id = frm.elements[i].value;
                break;
            }
        }
        if (payment_id == 0) {
            alert(payment_empty);
            return false;
        }
    }
    return true;
}
/* *
 *  处理用户添加一个红包
 */
function addBonus() {
    var frm = document.forms['addBouns'];
    var bonus_sn = frm.elements['bonus_sn'].value;
    if (bonus_sn.length == 0) {
        alert(bonus_sn_empty);
        return false;
    } else {
        var reg = /^[0-9]{10}$/;
        if (!reg.test(bonus_sn)) {
            alert(bonus_sn_error);
            return false;
        }
    }
    return true;
}
/* *
 *  合并订单检查
 */
function mergeOrder() {
    if (!confirm(confirm_merge)) {
        return false;
    }
    var frm = document.forms['formOrder'];
    var from_order = frm.elements['from_order'].value;
    var to_order = frm.elements['to_order'].value;
    var msg = '';
    if (from_order == 0) {
        msg += from_order_empty + '\n';
    }
    if (to_order == 0) {
        msg += to_order_empty + '\n';
    } else if (to_order == from_order) {
        msg += order_same + '\n';
    }
    if (msg.length > 0) {
        alert(msg);
        return false;
    } else {
        return true;
    }
}

/* *
 * 检测密码强度
 * @param       string     pwd     密码
 */
function checkIntensity(pwd) {
    var Mcolor = "#FFF", Lcolor = "#FFF", Hcolor = "#FFF", tip = "";
    var m = 0;
    var Modes = 0;
    for (i = 0; i < pwd.length; i++) {
        var charType = 0;
        var t = pwd.charCodeAt(i);
        if (t >= 48 && t <= 57) {
            charType = 1;
        } else if (t >= 65 && t <= 90) {
            charType = 2;
        } else if (t >= 97 && t <= 122)
            charType = 4;
        else
            charType = 4;
        Modes |= charType;
    }
    for (i = 0; i < 4; i++) {
        if (Modes & 1)
            m++;
        Modes >>>= 1;
    }
    if (pwd.length <= 4) {
        m = 1;
    }
    switch (m) {
        case 1 :
            Lcolor = "15px solid red";
            Mcolor = Hcolor = "15px solid #DADADA";
            tip = "密码强度：弱&nbsp;&nbsp;试试大小写字母和数字组合";
            break;
        case 2 :
            Lcolor = Mcolor = "15px solid #f90";
            Hcolor = "15px solid #DADADA";
            tip = "密码强度：中";
            break;
        case 3 :
            Lcolor = Mcolor = Hcolor = "15px solid #3c0";
            tip = "密码强度：强";
            break;
        case 4 :
            Lcolor = Mcolor = Hcolor = "15px solid #3c0";
            tip = "密码强度：强";
            break;
        default :
            Hcolor = Mcolor = Lcolor = tip = "";
            break;
    }
    if (document.getElementById("pwd_lower")) {
        document.getElementById("pwd_lower").style.borderBottom = Lcolor;
        document.getElementById("pwd_middle").style.borderBottom = Mcolor;
        document.getElementById("pwd_high").style.borderBottom = Hcolor;
        document.getElementById('pwd_stronger_tip').innerHTML = tip;
    }
}
function changeType(obj) {
    if (obj.getAttribute("min") && document.getElementById("ECS_AMOUNT")) {
        document.getElementById("ECS_AMOUNT").disabled = false;
        document.getElementById("ECS_AMOUNT").value = obj.getAttribute("min");
        if (document.getElementById("ECS_NOTICE") && obj.getAttribute("to") && obj.getAttribute('fee')) {
            var fee = parseInt(obj.getAttribute("fee"));
            var to = parseInt(obj.getAttribute("to"));
            if (fee < 0) {
                to = to + fee * 2;
            }
            document.getElementById("ECS_NOTICE").innerHTML = notice_result + to;
        }
    }
}
function calResult() {
    var amount = document.getElementById("ECS_AMOUNT").value;
    var notice = document.getElementById("ECS_NOTICE");
    reg = /^\d+$/;
    if (!reg.test(amount)) {
        notice.innerHTML = notice_not_int;
        return;
    }
    amount = parseInt(amount);
    var frm = document.forms['transform'];
    for (i = 0; i < frm.elements['type'].length; i++) {
        if (frm.elements['type'][i].checked) {
            var min = parseInt(frm.elements['type'][i].getAttribute("min"));
            var to = parseInt(frm.elements['type'][i].getAttribute("to"));
            var fee = parseInt(frm.elements['type'][i].getAttribute("fee"));
            var result = 0;
            if (amount < min) {
                notice.innerHTML = notice_overflow + min;
                return;
            }
            if (fee > 0) {
                result = (amount - fee) * to / (min - fee);
            } else {
                //result = (amount + fee* min /(to+fee)) * (to + fee) / min ;
                result = amount * (to + fee) / min + fee;
            }
            notice.innerHTML = notice_result + parseInt(result + 0.5);
        }
    }
}

function changeType_c () {
        if ("2" == $("#type").select().val()) {
            $(".company").show();
        } else {
            $("#com_name").val("");
            $("#com_tel").val("");
            $(".company").hide();
        }
        if ("2" == $("#f_type").select().val()) {
            $(".f_company").show();
        } else {
            $("#f_com_name").val("");
            $("#f_com_tel").val("");
            $(".f_company").hide();
        }
}
